package elements ;

import flash.display.Sprite;
import string.Utils;
import flash.net.URLLoader;
import flash.net.URLRequest;
import ColorConverter.Color;
import ColorConverter;

class Background extends Sprite {
	
		var color: Int;
		var img_x: Float;
		var img_y: Float;
	
	public function new( json:Dynamic )
	{
		super();
		if (Reflect.hasField(json, 'bg_color'))
			this.color = ColorConverter.toInt(Hex(json.bg_color)); //Utils.get_color( json.bg_color );
		else
			this.color = 0xf8f8d8;		// <-- default to Ivory
		
		if (Reflect.hasField(json,'bg_image'))
			this.load_img( json.bg_image );
		
	}
	
		function load_img( json:Dynamic ):Void {
		
		// added by NetVicious, 05 July, 2007 ++++
			
		if( Reflect.hasField(json,'bg_image_x'))
			this.img_x = json.bg_image_x;
				
		if( Reflect.hasField(json,'bg_image_y'))
			this.img_y = json.bg_image_y;
				
		//
		// LOAD THE IMAGE
		/*
		var loader:URLLoader;
		loader = new URLLoader();
		loader.addEventListener( Event.COMPLETE, imageLoaded );
		
		var loader:URLRequest = new URLRequest();
		loader.addListener({
			onLoadInit: function(mymc:MovieClip) {
				ref.positionize( mymc, ref.img_x, ref.img_y, new Square(0, 0, Stage.width, Stage.height) );
				delete loader;
			}
		});
			
		loader.loadClip(lv.bg_image, this.img_mc);
	*/
	}
	
	/*
	function xmlLoaded(event:Event):Void {
		var loader:URLLoader = URLLoader(event.target);
		loader.
	}

	// added by NetVicious, 05 July, 2007
	function positionize( mc:MovieClip, myX, myY, s:Square )
	{
		var X: Float = 0;
		var Y: Float = 0;

		if ( Math.isNaN(myX) ) {
			myX.toLowerCase()
			switch ( myX ) {
				case 'center':
					X = (s.width / 2) - (mc._width / 2);

				case 'left':
					X = s.left;

				case 'right':
					X = s.right - mc._width;

				default:
					X = 0;
			}
		} else if ( myX < 0 ) {
			X = s.right - mc._width - myX;
		} else { X = s.left + myX; }

		if ( Math.isNaN(myY) ) {
			myY.toLowerCase();
			switch ( myY ) {
				case 'middle':
					Y = (s.height / 2) - (mc._height / 2);

				case 'top':
					Y = s.top;

				case 'bottom':
					Y = s.bottom - mc._height;

				default:
					Y = 0;
			}
		} else if ( myY < 0 ) {
			Y = s.bottom - mc._height - myY;
		} else { Y = s.top + myY; }

		mc._x = X;
		mc._y = Y;
	}
	*/

	public function resize():Void {
		this.graphics.beginFill( this.color );
		this.graphics.drawRect( 0, 0, this.stage.stageWidth, this.stage.stageHeight );
	}
	
	public function die(): Void {
		this.graphics.clear();
	}
}
